package FIT::Document::Transformer::AddLink;

use FIT::Class;
use IO::All -utf8;
use FIT::Document::Parser::Twig;

extends 'FIT::Document::Transformer::Base';

has href => (is => 'ro', isa => 'IO::All', required => 1);

sub _transform {
    my $cell = shift;
    my $link = XML::Twig::Elt->new('a');
    $link->set_att(href => $self->href->name);

    # find element on which to stick a link- 1st non empty element
    # if not found just wrap entire cell in a link
    my $child;
    for $child ($cell->children) {
        next unless $child->trimmed_text;
        last;
    }

    # now add the link
    if ($child)
        { $link->merge($child) }
    else
        { $link->paste(first_child => $cell) }
}
